package com.dormitory.httpHelper;

import android.util.Log;

import com.dormitory.bean.DormitoryBean;
import com.dormitory.bean.NewsBean;
import com.dormitory.bean.NotificationBean;
import com.dormitory.bean.UserBean;
import com.dormitory.staticVariable.StaticVariable;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import static android.R.attr.id;

/**
 * Created by JHF on 2017/10/19.
 */

public class HttpGetInfo {
    public static DormitoryBean getDormitory(int id) throws IOException, JSONException {
        URL url = new URL(StaticVariable.GETDORMITORY_SERVLET);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("POST");
        conn.setReadTimeout(5000);
        conn.setConnectTimeout(5000);
        conn.setDoInput(true);
        conn.setDoOutput(true);
        conn.setUseCaches(false);
        OutputStream out = conn.getOutputStream();

        out.write((id+"").getBytes());
        out.flush();
        out.close();
        conn.connect();
        //Log.e("tag", conn.getResponseCode()+"");
        if(conn.getResponseCode() == 200) {
            InputStream is = conn.getInputStream();
            BufferedReader bf = new BufferedReader(new InputStreamReader(is));
            StringBuffer data0 = new StringBuffer();
            String buffer;
            while (((buffer) = bf.readLine()) != null) {
                data0.append(buffer);
            }
            JSONObject repJson = new JSONObject(data0.toString());
            DormitoryBean dormitory = new DormitoryBean();
            dormitory.setId(repJson.getInt("id"));
            dormitory.setName(repJson.getString("name"));
            return dormitory;
        }
        return null;
    }
    public static List<NotificationBean> getNotifications(int dormitoryId) throws IOException, JSONException {
        URL url = new URL(StaticVariable.GETNOTIFICATIONS_SERVLET);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("POST");
        conn.setReadTimeout(5000);
        conn.setConnectTimeout(5000);
        conn.setDoInput(true);
        conn.setDoOutput(true);
        conn.setUseCaches(false);
        OutputStream out = conn.getOutputStream();

        out.write((dormitoryId+"").getBytes());
        out.flush();
        out.close();
        conn.connect();
        //Log.e("tag", conn.getResponseCode()+"");
        if(conn.getResponseCode() == 200) {
            InputStream is = conn.getInputStream();
            BufferedReader bf = new BufferedReader(new InputStreamReader(is));
            StringBuffer data0 = new StringBuffer();
            String buffer;
            while (((buffer) = bf.readLine()) != null) {
                data0.append(buffer);
            }
            JSONArray repJson = new JSONArray(data0.toString());
            List<NotificationBean> list = new ArrayList<>();
            int l = repJson.length();
            JSONObject json;
            NotificationBean notificationBean;
            for(int i=0;i<l;i++) {
                json = repJson.getJSONObject(i);
                notificationBean = new NotificationBean();
                notificationBean.setId(json.getInt("id"));
                notificationBean.setUserName(json.getString("userName"));
                notificationBean.setTitle(json.getString("title"));
                notificationBean.setContent(json.getString("content"));
                notificationBean.setDormitoryId(json.getInt("dormitoryId"));
                list.add(notificationBean);
            }
            return list;
        }
        return null;
    }
    public static List<DormitoryBean> getFriendDormitory(int dormitoryId) throws IOException, JSONException {
        URL url = new URL(StaticVariable.GETFRIENDDORMITORY_SERVLET);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("POST");
        conn.setReadTimeout(5000);
        conn.setConnectTimeout(5000);
        conn.setDoInput(true);
        conn.setDoOutput(true);
        conn.setUseCaches(false);
        OutputStream out = conn.getOutputStream();

        out.write((dormitoryId+"").getBytes());
        out.flush();
        out.close();
        conn.connect();
        //Log.e("tag", conn.getResponseCode()+"");
        if(conn.getResponseCode() == 200) {
            InputStream is = conn.getInputStream();
            BufferedReader bf = new BufferedReader(new InputStreamReader(is));
            StringBuffer data0 = new StringBuffer();
            String buffer;
            while (((buffer) = bf.readLine()) != null) {
                data0.append(buffer);
            }
            JSONArray repJson = new JSONArray(data0.toString());
            List<DormitoryBean> list = new ArrayList<>();
            int l = repJson.length();
            JSONObject json;
            DormitoryBean dormitoryBean;
            for(int i=0;i<l;i++) {
                json = repJson.getJSONObject(i);
                dormitoryBean = new DormitoryBean();
                dormitoryBean.setId(json.getInt("id"));
                dormitoryBean.setName(json.getString("name"));
                list.add(dormitoryBean);
            }
            return list;
        }
        return null;
    }

    public static List<UserBean> getMember(int dormitoryId) throws JSONException, IOException {
        URL url = new URL(StaticVariable.GETMEMBER_SERVLET);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("POST");
        conn.setReadTimeout(5000);
        conn.setConnectTimeout(5000);
        conn.setDoInput(true);
        conn.setDoOutput(true);
        conn.setUseCaches(false);
        OutputStream out = conn.getOutputStream();

        out.write((dormitoryId+"").getBytes());
        out.flush();
        out.close();
        conn.connect();
        //Log.e("tag", conn.getResponseCode()+"");
        if(conn.getResponseCode() == 200) {
            InputStream is = conn.getInputStream();
            BufferedReader bf = new BufferedReader(new InputStreamReader(is));
            StringBuffer data0 = new StringBuffer();
            String buffer;
            while (((buffer) = bf.readLine()) != null) {
                data0.append(buffer);
            }
            JSONArray repJson = new JSONArray(data0.toString());
            List<UserBean> list = new ArrayList<>();
            int l = repJson.length();
            JSONObject json;
            UserBean userBean;
            for(int i=0;i<l;i++) {
                json = repJson.getJSONObject(i);
                userBean = new UserBean();
                userBean.setName(json.getString("name"));
                userBean.setId(json.getInt("id"));
                userBean.setTelephone(json.getString("telephone"));
                userBean.setDormitoryId(json.getInt("dormitoryId"));
                list.add(userBean);
            }
            return list;
        }
        return null;
    }
    public static List<NewsBean> getNews() throws IOException, JSONException {
        URL url = new URL(StaticVariable.GETNEWS_SERVLET);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("POST");
        conn.setReadTimeout(5000);
        conn.setConnectTimeout(5000);
        conn.setDoInput(true);
        conn.setDoOutput(true);
        conn.setUseCaches(false);
        conn.connect();
        //Log.e("tag", conn.getResponseCode()+"");
        if(conn.getResponseCode() == 200) {
            InputStream is = conn.getInputStream();
            BufferedReader bf = new BufferedReader(new InputStreamReader(is));
            StringBuffer data0 = new StringBuffer();
            String buffer;
            while (((buffer) = bf.readLine()) != null) {
                data0.append(buffer);
            }
            JSONArray repJson = new JSONArray(data0.toString());
            List<NewsBean> list = new ArrayList<>();
            int l = repJson.length();
            JSONObject json;
            NewsBean newsBean;
            for(int i=0;i<l;i++) {
                json = repJson.getJSONObject(i);
                newsBean = new NewsBean();
                newsBean.setId(json.getInt("id"));
                newsBean.setUserName(json.getString("userName"));
                newsBean.setContent(json.getString("content"));
                list.add(newsBean);
            }
            return list;
        }
        return null;
    }
}
